package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.userInfo.entity.DirectMessage;
import com.rongshu.view.userInfo.listener.OnMsgFunctionClickListener;
import com.utils.StringUtils;
import com.widget.CircleImageView;
import com.widget.SwipeMenuView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DirectMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;
    private List<DirectMessage.ResponseBean> directMessageList = null;
    //interface
    private OnMsgFunctionClickListener mOnMsgFunctionListener = null;

    public DirectMessageAdapter(Context context, List<DirectMessage.ResponseBean> directMessageList) {
        this.mContext = context;
        this.directMessageList = directMessageList;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_direct_message_item_view, parent, false);
        return new DirectMessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        try {
            DirectMessage.ResponseBean responseBean = directMessageList.get(position);
            final DirectMessageViewHolder holder = (DirectMessageViewHolder) viewHolder;
            //头像
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.drawable.user_head_icon);
            Glide.with(mContext)
                    .load(responseBean.getAvatar())
                    .apply(options)
                    .into(holder.iv_head_icon);
            holder.iv_vip.setVisibility(responseBean.isVip() ? View.VISIBLE : View.GONE);
            //name
            holder.txt_article_author.setText(responseBean.getBi_name());
            //content
            DirectMessage.ResponseBean.PrivatechatListBean privatechatListBean = responseBean.getPrivatechatList();
            holder.txt_article_content.setText(privatechatListBean != null ?
                    StringUtils.decoderMsg(privatechatListBean.getContents()) : "");
            if (privatechatListBean != null) {
                //time
                String time = privatechatListBean.getCreate_datetime();
                holder.txt_article_time.setText(DateUtils.ConvertDate(time));
                //count
                holder.txt_article_number.setText(responseBean.getCount() + "");
                holder.txt_article_number.setVisibility(responseBean.getNewSend_userid() == UserInfoUtils.getUserId() ?
                        View.GONE : responseBean.getCount() > 0 ? View.VISIBLE : View.GONE);
                //listener
                holder.layout_direct_message.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnMsgFunctionListener != null) {
                            mOnMsgFunctionListener.onItemClick(position);
                        }
                    }
                });
                //delete
                holder.btn_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnMsgFunctionListener != null) {
                            holder.menuView.quickClose();
                            mOnMsgFunctionListener.onItemDeleteClick(position);
                        }
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public int getItemCount() {
        return directMessageList == null ? 0 : directMessageList.size();
    }

    public class DirectMessageViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.layout_direct_message)
        RelativeLayout layout_direct_message;
        @BindView(R.id.iv_head_icon)
        CircleImageView iv_head_icon;
        @BindView(R.id.iv_vip)
        ImageView iv_vip;
        @BindView(R.id.txt_article_author)
        TextView txt_article_author;
        @BindView(R.id.txt_article_content)
        TextView txt_article_content;
        @BindView(R.id.txt_article_time)
        TextView txt_article_time;
        @BindView(R.id.txt_article_number)
        TextView txt_article_number;
        @BindView(R.id.menuView)
        SwipeMenuView menuView;
        @BindView(R.id.btn_delete)
        TextView btn_delete;

        public DirectMessageViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemClickListener(OnMsgFunctionClickListener listener) {
        this.mOnMsgFunctionListener = listener;
    }
}
